/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.Date;
import javax.ejb.Stateless;
import javax.inject.Named;

/**
 *
 * @author Emergenza
 */
@Named ("eventBean")
@Stateless
public class EventBean {
    private String nome;
    private int type;
    private Date begDate;
    private Date endDate;
    private String descr;
    private String city;
    private String country;
    private String address;
    private String weather;

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }
    private String showParticipants = "";
    
    

    public String getShowParticipants() {
        return showParticipants;
    }

    public void setShowParticipants(String showParticipants) {
        if(checkRegExp(showParticipants)){
            this.showParticipants = this.showParticipants + showParticipants + " \n";
        }
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr, String city, String country, String address, String showParticipants, String temp) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
        this.city = city;
        this.country = country;
        this.address = address;
        this.showParticipants = "";
        this.temp = temp;
    }
    private String temp;

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr, String city, String country, String address, String temp) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
        this.city = city;
        this.country = country;
        this.address = address;
        this.temp = temp;
    }

    public String getDescr() {
        return descr;
    }

    public void setDescr(String descr) {
        this.descr = descr;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr, String city, String country, String address) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
        this.city = city;
        this.country = country;
        this.address = address;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr, String city, String country) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
        this.city = city;
        this.country = country;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr, String city) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
        this.city = city;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate, String descr) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
        this.descr = descr;
    }

    public Date getBegDate() {
        return begDate;
    }

    public void setBegDate(Date begDate) {
        this.begDate = begDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public EventBean(String nome, int type, Date begDate, Date endDate) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
        this.endDate = endDate;
    }

    public EventBean(String nome, int type, Date begDate) {
        this.nome = nome;
        this.type = type;
        this.begDate = begDate;
    }

    public EventBean(String nome, int type) {
        this.nome = nome;
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    
    
    public EventBean() {
    }

    public EventBean(String nome) {
        this.nome = nome;
    }
    

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
    
    public String save(){
        System.out.println(nome +"   " + type + "    " + begDate + "    " + endDate);
        if(check()){
            return ("homepage?faces-redirect=true"); //redirect to homepage
        }else{
            return("eventcreation?faces-redirect=true");
        }
    }

    private boolean check() {
        boolean result = false;
        if(nome.length()>= 0 && type != 0 && country.length()>= 0 && city.length() >= 0 && begDate != null && endDate != null){
            if(begDate.before(endDate)){
                result = true;
            }
        }
        return result;
    }

    private boolean checkRegExp(String showParticipants) {
        char c;
        boolean at = false, dot = false, error = false;
        for(int i = 0; i < showParticipants.length(); i++){
            c = showParticipants.charAt(i);
            if(c == '@'){
                at = true;
            }
            if(dot && c =='.'){
                error = true;
            }
            if(at && c =='.'){
                dot = true;
            }
        }
        System.out.println("regexp " + dot + " "+ at +" "+ error);
        return (dot && !error);
    }
}
